<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Category;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     *获取分类
     *
     */
    public function get_category_lv1()
    {
            $list = Category::where('pid',0)->order('weigh','desc')->field('name,id,path')->select();
            if(count($list) <= 0){
                $this->success('成功',['list' => []]);
            }
            $category_ids = array_column($list,'id');
            $son_list = Category::whereIn('pid',$category_ids)->group('pid')->column('pid');
            foreach ($list as &$value){
                $value['is_tab'] = in_array($value['id'],$son_list);
            }
            $this->success('成功',['list' => $list]);
    }
    public function get_category_lv2()
    {
        $category_id = $this->request->get('category_id');
        $list = Category::where('pid',$category_id)->order('weigh','desc')->field('name,id,path')->select();
        if(count($list) <= 0){
            $this->success('成功',['list' => []]);
        }
        $category_ids = array_column($list,'id');
        $son_list = Category::whereIn('pid',$category_ids)->group('pid')->column('pid');
        foreach ($list as &$value){
            $value['is_tab'] = in_array($value['id'],$son_list);
        }
        $this->success('成功',['list' => $list]);
    }
    public  function get_tab(){
        $category_id = $this->request->get('category_id');
        $list = Category::where('pid',$category_id)->where('type','tab')->order('weigh','desc')->field('name,id,path')->select();
        $this->success('成功',['list' => $list]);
    }
    public function contact_information(){
        $this->success('成功',[
            'address' => config('site.address'),
            'email' => config('site.email'),
            'mobile' => config('site.mobile'),
            'wx_code' =>  $this->request->domain().config('site.wx_code'),
            'qq_code' => $this->request->domain().config('site.qq_code')
        ]);
    }
}
